<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
      
      <FormItem name="mobile" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.mobile"
          :placeholder="t('sys.login.mobile')"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem name="regValidCode" class="enter-x">
        <CountdownInput
          size="large"
          class="fix-auto-fill"
          v-model:value="formData.regValidCode"
          :sendCodeApi="yztelnumandsend"
          :placeholder="t('sys.login.smsCode')"
        />
      </FormItem>
     
      <FormItem name="password" class="enter-x">
        <StrengthMeter
          size="large"
          v-model:value="formData.password"
          :placeholder="t('sys.login.password')"
        />
      </FormItem>
      <FormItem name="confirmPassword" class="enter-x">
        <InputPassword
          size="large"
          visibilityToggle
          v-model:value="formData.confirmPassword"
          :placeholder="t('sys.login.confirmPassword')"
          autocomplete="false"
        />
      </FormItem>

       <FormItem name="yqm" class="enter-x">
        <Input
          size="large"
          v-model:value="formData.yqm"
          placeholder="输入邀请码,如无可不输入"
          class="fix-auto-fill"
        />
      </FormItem>

      <FormItem class="enter-x" name="policy">
        <!-- No logic, you need to deal with it yourself -->
        <Checkbox v-model:checked="formData.policy" size="small">
           我同意平台相关隐私政策
        </Checkbox>
      </FormItem>

     
      <Button
        type="primary"
        class="enter-x"
        size="large"
        block
        @click="handleRegister"
        :loading="loading"
      >
        {{ t('sys.login.registerButton') }}
      </Button>
      <Button size="large" block class="mt-4 enter-x" @click="handleBackLogin">
        {{ t('sys.login.backSignIn') }}
      </Button>
    </Form>
  </template>
</template>
<script lang="ts" setup>
  import { reactive, ref, unref, computed } from 'vue';
  import LoginFormTitle from './LoginFormTitle.vue';
  import { Form, Input, Button, Checkbox } from 'ant-design-vue';
  import { StrengthMeter } from '/@/components/StrengthMeter';
  import { CountdownInput } from '/@/components/CountDown';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useLoginState, useFormRules, useFormValid, LoginStateEnum } from './useLogin';
  import { getRegSmscode, saveRegNew } from '/@/api/sys/account';
  import { useRoute } from 'vue-router';
  const FormItem = Form.Item;
  const InputPassword = Input.Password;
  const { t } = useI18n();
  const { handleBackLogin, getLoginState } = useLoginState();

  const route = useRoute()

  const formRef = ref();

  const { showMessage, notification } = useMessage();
  
  const loading = ref(false);

  const formData = reactive({
    loginCode: '',
    password: '',
    confirmPassword: '',
    mobile: '',
    regValidCode: '',
    policy: false,
    yqm: route.query.sharecode||'',
  });

  const { getFormRules } = useFormRules(formData);
  const { validForm } = useFormValid(formRef);

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.REGISTER);

  async function handleRegister() {
    formData.loginCode=formData.mobile;
    const data = await validForm();
    if (!data) return;
    loading.value=true;
    //开始提交啊
    try{
     const res = await saveRegNew(formData);
        if (res.result == 'true') {
       
        notification.success({
            message: '注册成功',
            description: '您可以使用账号密码登录系统',
            duration: 3,
          });
          //清空
            formData.loginCode='';
            formData.mobile='';
            formData.regValidCode='';
            formData.password='';
            formData.confirmPassword='';
            formData.yqm='';
            //返回登录框
            handleBackLogin();
      }
     }catch(error: any){

    }finally{
      loading.value=false;
    }
  }

  const yztelnumandsend = async () => {
       const regex = /^1[3-9]\d{9}$/; // 中国大陆手机号码正则表达式
      if (regex.test(formData.mobile.trim())) {
          const res = await getRegSmscode({telnum:formData.mobile.trim(),type:'reg'});
          return res.result=='true'
      } else {
        showMessage('请填写正确的手机号');
      }
      return false;
  };
</script>
